﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.CardArrival;

namespace CardLogistics.DAL.CardArrival
{
    /// <summary>
    /// 订到货管理台账 数据访问类
    /// </summary>
    public class OrderArrivalBookData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_OrderArrivalBook", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OrderArrivalBook GetModel(string rId)
        {
            OrderArrivalBook model = new OrderArrivalBook();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["ORDERID"] != null)
                {
                    model.ORDERID = row["ORDERID"].ToString();
                }
                if (row["CHECKID"] != null)
                {
                    model.CHECKID = row["CHECKID"].ToString();
                }
                if (row["ORDERDATE"] != null && row["ORDERDATE"].ToString() != "")
                {
                    model.ORDERDATE = DateTime.Parse(row["ORDERDATE"].ToString());
                }
                if (row["STORAGEDATE"] != null && row["STORAGEDATE"].ToString() != "")
                {
                    model.STORAGEDATE = DateTime.Parse(row["STORAGEDATE"].ToString());
                }
                if (row["ORDERNUMBER"] != null && row["ORDERNUMBER"].ToString() != "")
                {
                    model.ORDERNUMBER = decimal.Parse(row["ORDERNUMBER"].ToString());
                }
                if (row["UNITPRICE"] != null && row["UNITPRICE"].ToString() != "")
                {
                    model.UNITPRICE = decimal.Parse(row["UNITPRICE"].ToString());
                }
                if (row["TOTALPRICE"] != null && row["TOTALPRICE"].ToString() != "")
                {
                    model.TOTALPRICE = decimal.Parse(row["TOTALPRICE"].ToString());
                }
                if (row["CHECKNUMBER"] != null && row["CHECKNUMBER"].ToString() != "")
                {
                    model.CHECKNUMBER = decimal.Parse(row["CHECKNUMBER"].ToString());
                }
                if (row["MISSINGCARDNO"] != null)
                {
                    model.MISSINGCARDNO = row["MISSINGCARDNO"].ToString();
                }
                if (row["REMARK"] != null)
                {
                    model.REMARK = row["REMARK"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                #region 添加便于查询的字段
                if (row["OrderNo"] != null && row["OrderNo"].ToString() != "")
                {
                    model.OrderNo = row["OrderNo"].ToString();//订单编号
                }
                if (row["CheckNo"] != null && row["CheckNo"].ToString() != "")
                {
                    model.CheckNo = row["CheckNo"].ToString();//入库单号
                }
                #endregion
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(OrderArrivalBook model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					 new OracleParameter("vORDERID", OracleType.VarChar,50),
					new OracleParameter("vCHECKID", OracleType.VarChar,50),
					new OracleParameter("vORDERDATE", OracleType.DateTime),
					new OracleParameter("vSTORAGEDATE", OracleType.DateTime),
					new OracleParameter("vORDERNUMBER", OracleType.Number,4),
					new OracleParameter("vUNITPRICE", OracleType.Number,4),
					new OracleParameter("vTOTALPRICE", OracleType.Number,4),
					new OracleParameter("vCHECKNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.VarChar,200),
                    new OracleParameter("vREMARK", OracleType.NVarChar),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.ORDERID;
                parameters[2].Value = model.CHECKID;
                parameters[3].Value = model.ORDERDATE;
                parameters[4].Value = model.STORAGEDATE;
                parameters[5].Value = model.ORDERNUMBER;
                parameters[6].Value = model.UNITPRICE;
                parameters[7].Value = model.TOTALPRICE;
                parameters[8].Value = model.CHECKNUMBER;
                parameters[9].Value = model.MISSINGCARDNO;
                parameters[10].Value = model.REMARK;
                parameters[11].Value = model.LASTUPDATEUSER;
                parameters[12].Value = model.LASTUPDATEDATE;
                string StrOutPar = DbHelperSQL.RunProcedure("P_OrderArrivalBook_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(OrderArrivalBook model)
        {
            try
            {
                     OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vORDERID", OracleType.VarChar,50),
					new OracleParameter("vCHECKID", OracleType.VarChar,50),
					new OracleParameter("vORDERDATE", OracleType.DateTime),
					new OracleParameter("vSTORAGEDATE", OracleType.DateTime),
					new OracleParameter("vORDERNUMBER", OracleType.Number,4),
					new OracleParameter("vUNITPRICE", OracleType.Number,4),
					new OracleParameter("vTOTALPRICE", OracleType.Number,4),
					new OracleParameter("vCHECKNUMBER", OracleType.Number,4),
					new OracleParameter("vMISSINGCARDNO", OracleType.VarChar,200),
                    new OracleParameter("vREMARK", OracleType.NVarChar),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                  	parameters[0].Value = model.ID;
			        parameters[1].Value = model.ORDERID;
			        parameters[2].Value = model.CHECKID;
			        parameters[3].Value = model.ORDERDATE;
			        parameters[4].Value = model.STORAGEDATE;
			        parameters[5].Value = model.ORDERNUMBER;
			        parameters[6].Value = model.UNITPRICE;
			        parameters[7].Value = model.TOTALPRICE;
			        parameters[8].Value = model.CHECKNUMBER;
			        parameters[9].Value = model.MISSINGCARDNO;
            		parameters[10].Value = model.REMARK;
			        parameters[11].Value = model.LASTUPDATEUSER;
			        parameters[12].Value = model.LASTUPDATEDATE;
                     parameters[13].Direction = ParameterDirection.Output;
                     string StrOutPar = DbHelperSQL.RunProcedure("P_OrderArrivalBook_Update", parameters, parameters[13]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_OrderArrivalBook_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
